package com.nvwa.pagehelper;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * PageInfoBuilder 类。
 *
 * @author wangjing
 * @date 2024-08-12 [wangjing] 创建。
 */
public class PageInfoBuilder<E, T> {

    private Page<E> page;

    private List<T> newList;

    public PageInfoBuilder() {
    }

    public PageInfoBuilder<E, T> setPage(Page<E> page) {
        this.page = page;
        return this;
    }

    public PageInfoBuilder<E, T> setNewList(List<T> newList) {
        this.newList = newList;
        return this;
    }

    /**
     * 构建分页对象
     */
    public PageInfo<T> build() {
        PageInfo<T> pageInfo = new PageInfo<>(this.newList);
        pageInfo.setPageNum(page.getPageNum());
        pageInfo.setPageSize(page.getPageSize());
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    public static <E, T> PageInfoBuilder<E, T> newBuilder(Page<E> page, List<T> newList) {
        return new PageInfoBuilder<E, T>().setPage(page).setNewList(newList);
    }

}
